package com.readaloud.wicket;


import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;

import com.readaloud.hibernate.user.Books;

@SuppressWarnings("unchecked")
public class ListBooks extends HomePage{

	/**
	 * 
	 */
	private static final long serialVersionUID = 3310030803418235277L;
	
	
	public ListBooks(PageParameters params){
		final String searchBook = params.get("searchBook").toString();
		System.out.println("Search Book: "+ searchBook);
        IModel invertedIndexModel = new LoadableDetachableModel() {
            protected Object load() {
                return WicketApplication.get().getInvertedIndexDao().query(searchBook);
            }
        };
        
		ListView document = new ListView("books", invertedIndexModel) {
            protected void populateItem(ListItem item) {
                Link view = new Link("view", item.getModel()) {
                    public void onClick() {
                        setResponsePage(new ViewBook((Books)getModelObject()));
                    }
                };
                view.add(new Label("name",
                    new PropertyModel(item.getModel(), "name")));
                item.add(view);
                
            }        };
        add(document);
	}
}
